/*
 * @Descripttion: 
 * @Author: 周杨
 * @Date: 2021-08-09 18:17:36
 * @LastEditors: 周杨
 * @LastEditTime: 2021-09-22 16:25:54
 */
import axios from 'axios';
import NProgress from 'nprogress';
import { getStore } from '@/utils/store';
import { Message } from 'element-ui';

NProgress.configure({ showSpinner: false })

axios.defaults.baseURL = window.baseURL;
// 添加请求拦截器
axios.interceptors.request.use((config) => {
	NProgress.start();
	config.headers['Authorization'] = 'Bearer ' + getStore('Ticket') || '';
	return config;
}, function (error) {
	NProgress.done();
	alert(error.message)
	return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use((response) => {
	NProgress.done();
	if (response.status == 200) {
		if (response.data.msgCode == "0000") {
			return response;
		}
	}
	alert(response.data.msg || '服务器异常')
	return Promise.reject(response.data.msg || '服务器异常');
}, function (error) {
	NProgress.done();
	alert(error.message)
	return Promise.reject(error);
});
function alert(msg) {
	Message.error({
		message: msg || '服务器异常',
		duration: 1500,
		showClose: true
	})
}
export default axios;